package com.vken.blog.dao.hibernate;

import java.util.List;

import com.vken.blog.dao.CommentDao;
import com.vken.blog.model.Comment;
import com.vken.blog.util.Pagination;

public class CommentDaoHibernate extends GenericDaoHibernate<Comment, Integer> implements
		CommentDao {
	public List<Comment> getCommentsByEntryId(Integer id) {
		return getHibernateTemplate().find(
				"from Comment as c where c.entry.id=? and c.status=1 order by c.id desc",
				new Object[] { id });
	}

	public Pagination getPaginationByBlogId(Integer id, Integer status, int pageSize, int pageNumber) {
		StringBuffer sb = new StringBuffer();
		sb.append("from Comment as c where c.entry.category.blog.id=");
		sb.append(id);
		if (status != null) {
			sb.append(" and c.status=");
			sb.append(status);
		}

		int totalCount = ((Long) queryObject("select count(*) " + sb.toString(), null)).intValue();
		return getPagination(sb.toString() + " order by c.id desc", null, pageSize, pageNumber,
				totalCount);
	}

	public void deleteByEntryId(Integer id) {
		getHibernateTemplate().bulkUpdate(
				"delete from Comment c where c.entry.id=? and c.status=1", new Object[] { id });

	}
}
